package com.zmn.oms.third.shunfeng.service.impl;

import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.third.shunfeng.ShunfengConsts;
import com.zmn.oms.third.shunfeng.service.ShunfengService;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 顺丰安装渠道对接订单状态处理实现
 *
 * @author xiewenbing
 * @date  2020/12//09 17:00
 */
@Slf4j
@Service("shunfengNotifyService")
public class ShunfengNotifyServiceImpl implements OrderChangeNotifyService {

    @Autowired
    protected ShunfengService shunfengService;

    @Override
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {

        // 判断功能是否开启
        if (!ShunfengConsts.POWER_ON) {
            log.info("【{}】<订单状态同步> 该功能没有开启。", ShunfengConsts.CHANNEL_NAME);
            return;
        }

        if (!Objects.equals(orderWorkLogChange.getInputType(),OrderConsts.ORDER_INPUT_TYPE_API)) {
            log.info("【{}】的录入方式不为API接入，不需要状态同步", ShunfengConsts.CHANNEL_NAME);
            return;
        }

        log.info("【{}】<订单状态同步> ,状态信息:[{}]。", ShunfengConsts.CHANNEL_NAME,orderWorkLogChange.toString());

        int logType = orderWorkLogChange.getLogType().intValue();

        try {
            switch (logType) {

                // 派单
                case OrderLogConsts.ORDER_LOG_TYPE_DISTRIBUTE:
                    shunfengService.syncOrderDistribute(orderWorkLogChange);
                    break;

                // 修改、改约
                case OrderLogConsts.ORDER_LOG_TYPE_CONFIRM:
                case OrderLogConsts.ORDER_LOG_TYPE_UPDATE:
                case OrderLogConsts.ORDER_LOG_TYPE_MODIFY_DUTY_TIME:
                case OrderLogConsts.ORDER_LOG_TYPE_MASTER_MODIFY_DUTY_TIME:
                    shunfengService.syncOrderDutytime(orderWorkLogChange);
                    break;

                // 服务完成
                case OrderLogConsts.ORDER_LOG_TYPE_SERVICE_COMPLETE:
                    shunfengService.syncOrderComplete(orderWorkLogChange);
                    break;
                default:
                    log.info("【{}】<订单状态-【{}】不同步> ", ShunfengConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType));
                    return;
            }
        } catch (OmsBaseException exception) {

            log.info("【{}】<订单状态-【{}】同步> 失败 【{}】 ", ShunfengConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType),
                    exception.getMessage());
        }

        log.info("【{}】<订单状态-【{}】同步> 完成 ", ShunfengConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType));
    }
}
